using Microsoft.AspNetCore.Mvc;     // 因为ControllerBase等需要引入
using MyWebApi.Api.Entity;      // 因为Users表需要引入
using System.Collections.Generic;   // 因为泛型需要引入
using System.Linq;     // 因为Where 引入之后我们可以使用一种类似SQL的语法来查询任何形式的数据
using System;
using MyWebApi.Api.Repository;
using MyWebApi.Api.ParamModel;
using MyWebApi.Api.Utils;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;


// 
// 商品控制器
// 

namespace MyWebApi.Api.Controllers
{
    // 特性
    // [Authorize]  // [Authorize]下的所有Api接口如：查询、删除、创建用户不能匿名访问，需要Token令牌才可访问
    [ApiController]
    [Route("[controller]")]

    public class SearchController : ControllerBase
    {

        // 实例化数据库
        // private MyWebApi.Api.Db.MyWebApiDb db = new Db.MyWebApiDb();

        // 定义为IRepository<T>的泛型接口未知数 T 为 users
        private IRepository<users> _usersRespository;   // 现在_usersRespository里包含IRepository<T>泛型接口里面的属性(可以使用这些属性来进行对Users表的操作)
        private IRepository<commodity> _commodity;   // 商品表
        private IRepository<commodity_property> _commodity_property;
        private IRepository<commodity_property_vel> _commodity_property_vel;
        // 文件上传需要
        private readonly IWebHostEnvironment _hostingEnvironment;


        private TokenParameter _tokenParameter;
        private readonly IConfiguration _configuration;

        // 一个构造函数：初始化UsersController控制器，使该控制器一开始就有以下的属性
        public SearchController(IConfiguration configuration,
        IWebHostEnvironment hostingEnvironment,
         IRepository<users> usersRespository,
         IRepository<commodity> commodity,
         IRepository<commodity_property> commodity_property,
         IRepository<commodity_property_vel> commodity_property_vel

         )
        {
            // 文件上传需要
            _hostingEnvironment = hostingEnvironment;

            _configuration = configuration;
            _tokenParameter = _configuration.GetSection("tokenParameter").Get<TokenParameter>();

            _usersRespository = usersRespository;
            _commodity = commodity;
            _commodity_property = commodity_property;
            _commodity_property_vel = commodity_property_vel;

        }


        // 模糊搜索商品
        [HttpPost, Route("vague_sel_commodity")]
        public dynamic selCommodity(SearchModel model)
        {
            // 商品关键字
            var commodityKeyword = model.CommodityKeyword;


            var commoditys = from allCommodity in _commodity.Table
                             join commodity_property in _commodity_property.Table on allCommodity.Id equals commodity_property.commodityId
                             join commodity_property_vel in _commodity_property_vel.Table on commodity_property.Id equals commodity_property_vel.commodityNormsId
                             where allCommodity.IsDeleted == false && allCommodity.Status == true && allCommodity.commodityName.Contains(commodityKeyword) || allCommodity.Introduce.Contains(commodityKeyword)
                             select new
                             {
                                 CommodityId = allCommodity.Id,
                                 CommodityName = allCommodity.commodityName,
                                 Introduce = allCommodity.Introduce,
                                 SurfacePlot = allCommodity.surfacePlot,
                                 SupplierId = allCommodity.supplierId,
                                 ClassifyId = allCommodity.classifyId,
                                 CreatedTime = allCommodity.CreatedTime,

                                 Repertory = commodity_property_vel.repertory == 0 ? 0 : commodity_property_vel.repertory,
                                 Money = commodity_property_vel.money == 0 ? 0 : commodity_property_vel.money,

                             };

            var OrderByCommodityJoinProperty = commoditys.OrderByDescending(x => x.CreatedTime).ToList();



            if (commoditys.Count() > 0)
            {
                var res = new
                {
                    Code = 1000,
                    Data = commoditys,
                    Msg = "搜索商品成功"
                };
                return JsonHelper.Serialize(res);
            }
            else
            {
                var res = new
                {
                    Code = 1001,
                    Data = "抱歉，店小二没找到您要的商品",
                    Msg = "抱歉，店小二没找到您要的商品"
                };
                return JsonHelper.Serialize(res);
            }

        }









    }
}